<script lang="ts" setup>
import type { VxeGridProps } from '@/components/Basic';
import { onMounted, useTemplateRef } from 'vue';
import { BasicPage, useBasicTable } from '@/components/Basic';

interface RowType {
  category: string;
  color: string;
  id: string;
  price: string;
  productName: string;
  releaseDate: string;
}

const gridOptions: VxeGridProps<RowType> = {
  checkboxConfig: {
    highlight: true,
    labelField: 'name',
  },
  columns: [
    { title: '序号', type: 'seq', width: 50 },
    { align: 'left', title: 'Name', type: 'checkbox', width: 100 },
    {
      field: 'category',
      sortable: true,
      slots: { default: 'category' },
      title: 'Category',
    },
    { field: 'color', sortable: true, title: 'Color' },
    { field: 'productName', sortable: true, title: 'Product Name' },
    { field: 'price', sortable: true, title: 'Price' },
    { field: 'releaseDate', formatter: 'formatDateTime', title: 'DateTime' },
  ],
  exportConfig: {},
  height: 'auto',
  keepSource: true,
  proxyConfig: {
    ajax: {
      query: async ({ page, sort }) => {
        console.log(page, sort);
        // 模拟接口请求
        await new Promise((resolve) => setTimeout(resolve, 2000)); // 正确等待 500ms
        return {
          items: [
            { name: '张三' },
            { name: '李四' },
            { name: '王五' },
            { name: '赵六' },
          ],
          total: 100,
        };
      },
    },
    sort: true,
  },
  sortConfig: {
    defaultSort: { field: 'category', order: 'desc' },
    remote: true,
  },
  toolbarConfig: {
    custom: true,
    export: true,
    refresh: { code: 'query' },
    zoom: true,
  },
};

const [BasicTable, tableApi] = useBasicTable({
  gridOptions,
});
console.log(tableApi);
const tableRef = useTemplateRef('tableRef');
onMounted(() => {
  console.log(tableRef.value?.$options);
});
</script>

<template>
  <BasicPage auto-content-height>
    <BasicTable ref="tableRef">
      <template #category="{ row }">
        <div>{{ row.name }}</div>
      </template>
    </BasicTable>
  </BasicPage>
</template>
